package cn.itcast.gulimall.product.service.impl;

import cn.itcast.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.itcast.common.utils.PageUtils;
import cn.itcast.common.utils.Query;

import cn.itcast.gulimall.product.dao.CategoryDao;
import cn.itcast.gulimall.product.entity.CategoryEntity;
import cn.itcast.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        /**
        // 1、查询所有的分类
        List<CategoryEntity> entities = baseMapper.selectList(null);

        // 2、组装成父子的树形接口

        // 2.1 先找到所有的一级分类
        List<CategoryEntity> menus = entities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0).map(menu -> {
            menu.setChildren(getChildrenList(menu,entities));

            return menu;
        }).sorted((menu1,menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return menus;
         **/

        // 1、查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);

        // 2、组装成父子的树形结构

        // 2.1、找到所有的一级分类
        List<CategoryEntity> level1Mnues = entities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0).map(menu -> {
            menu.setChildren(getChildrens(menu,entities));
            return menu;
        }).sorted((menu1,menu2) -> {
            return (menu1.getSort() ==null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return level1Mnues;
    }

    @Override
    public void removeCategoryByIds(List<Long> asList) {
        //TODO 1、检查当前删除的菜单是否别的地方引用了
        baseMapper.deleteBatchIds(asList);
    }

    //[2,25,225]
    @Override
    public Long[] findCateLogPath(Long catelogId) {
        List<Long> paths = new ArrayList<Long>();
        List<Long> parentPath = findParentPath(catelogId,paths);

        Collections.reverse(parentPath);

        return (Long[]) parentPath.toArray(new Long[parentPath.size()]);
    }

    @Transactional
    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);

        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    // [225,25,2]
    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        //1、收集当前节点id
        paths.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            findParentPath(categoryEntity.getParentCid(),paths);
        }

        return paths;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity currentEntity,List<CategoryEntity> allEntities){
        /**
        List<CategoryEntity> children = allEntities.stream().filter(item -> {
            return item.getParentCid() == currentEntity.getCatId();
        }).map(menu -> {
            menu.setChildren(getChildrens(menu,allEntities));
            return menu;
        }).sorted((menu1,menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;
         **/
        List<CategoryEntity> children = allEntities.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == currentEntity.getCatId();
        }).map(categoryEntity -> {
            // 1、找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,allEntities));

            return categoryEntity;
        }).sorted((menu1,menu2) -> {
            // 2、菜单排序
            return (menu1.getSort() ==null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}